<?php
/**
 * @Author: zhangcc
 * @Date: 2022/8/12
 */

namespace app\libraries\utils;

class ImgUtil
{
    /**
     * 获取完整的图片路径
     * @param string $host
     * @param string $url
     * @return string
     */
    public static function completeUrl(string $host, string $url): string
    {
        if (empty($url)) {
            return '';
        }

        if (str_contains($url, 'http://')) {
            return $url;
        }

        if (str_contains($url, 'https://')) {
            return $url;
        }

        return rtrim($host, '/') . '/' . ltrim($url, '/');
    }

    /**
     * 删除图片域名
     * @param  string  $host
     * @param  string  $url
     * @return string
     */
    public static function removeDomain(string $host, string $url): string
    {
        if (str_contains($url, $host)) {
            $len = strlen($host);
            return substr($url, $len);
        }
        return $url;
    }
}